package datastructure.array;

/**
 * @author XY
 * @version 1.0
 * @date 2022/3/16 9:21
 * @Description
 */
public class Leet11 {


    //超时
    public static int maxArea1(int[] height) {

        int max = 0;
        for (int i = 0; i < height.length; i++) {
            for (int j = i+1; j < height.length; j++) {
                max = Math.max(max,(j-i)*Math.min(height[i],height[j]));
            }
        }
        return max;
    }

    //双指针贪心
    public static int maxArea(int[] height) {

        int i =0,j=height.length-1;
        int max = 0;
        while(i<j){
            int area = Math.max(max,(j-i)*Math.min(height[i],height[j]));
            max = Math.max(max,area);
            //移动指向元素较小的指针，如果移动元素大的指针，那么之后得到的结果永远小于现在的结果
            if(height[i]<=height[j]){
                i++;
            }else{
                j--;
            }

        }
        return max;
    }

    public static void main(String[] args) {
        int[] arr = {1,3,2,5,25,24,5};
        System.out.println(maxArea(arr));
    }
}
